package com.wallet.admin.controller.system;


import com.wallet.admin.feign.system.RoleFeignClient;
import com.wallet.common.util.PageUtil;
import com.wallet.common.util.StringUtil;
import com.wallet.common.vo.ApiResult;
import com.wallet.common.vo.DataResult;
import com.wallet.common.vo.PageResult;
import com.wallet.provider.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleFeignClient roleFeignClient;

    /**
     * 分页查询系统角色
     * @param offset
     * @param limit
     * @param order
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataResult<Role> list(String search, @RequestParam(defaultValue = "1") int offset, @RequestParam(defaultValue = "10")  int limit, @RequestParam(defaultValue = "createTime")String ordername, @RequestParam(defaultValue = "desc") String order){
        PageResult<Role> roles = roleFeignClient.getRoleList(search, PageUtil.getPageNum(offset, limit), limit, StringUtil.camel2Underline(ordername), order);
        return new DataResult.Builder().build(roles);
    }

    /**
     * 保存角色
     * @param role
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ApiResult add(Role role){
        Boolean rs = roleFeignClient.addRole(role);
        return new ApiResult.Builder<>().success(rs).build();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public ApiResult<Role> delete(String ids){
        Boolean rs = roleFeignClient.deleteRole(ids);
        return new ApiResult.Builder<>().success(rs).build();
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(Long id, Model model){
        Role role = roleFeignClient.getRoleById(id);
        model.addAttribute("role", role);
        return "system/role_edit";
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ApiResult<Role> update(Role role){
        Boolean rs = roleFeignClient.updateRole(role);
        return new ApiResult.Builder<>().success(rs).build();
    }
}

